package com.lysj.bhp.exchangerate.api;

import com.google.gson.Gson;
import com.lysj.bhp.api.base.ApiContent;
import com.lysj.bhp.exchangerate.json.bcex.*;
import com.lysj.bhp.exchangerate.json.digifinex.DigifinexRate;
import com.lysj.bhp.util.http.HttpUtil;
import com.lysj.bhp.util.web.ParamUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author zk
 * @description
 * @create 2018-07-25 10:22
 **/
@Service
public class DigifinexService extends ApiContent {

    private static Logger logger = LoggerFactory.getLogger(DigifinexService.class);
    private static String URL = "https://www.digifinex.com/api/ticker";
    private static String DOLLAR_TO_CNY_URL = "https://www.bcex.top/coins/getBtcCny?type=bcex";
    /*D网,钻石比特币钱包跟布比钱包的汇率,暂时用bcex代理URL*/
    private static String  BCEX_WALLET_TO_Dollar_URL = "http://120.77.85.235:8098/bcex/markets";


    /**
     * 获取以太坊币与美元的汇率
     *
     * @return
     */
    public BigDecimal getEthereumToDollar() {
        BigDecimal rate = new BigDecimal("-1");
        String respJson = HttpUtil.sendGet(URL, "symbol=usdt_eth&appKey=59328e10e296a&sign=0a8d39b515fd8f3f8b848a4c459884c2");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        DigifinexRate digifinexRate = new Gson().fromJson(respJson, DigifinexRate.class);
        if (digifinexRate == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }

        BigDecimal dataLast = digifinexRate.getTicker().getLast();
        if (dataLast == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        logger.info("[D网内部接口]以太币对应美元汇率:" + dataLast);
        rate = dataLast;
        return rate;
    }

    /**
     * 获取比特币与美元的汇率
     *
     * @return
     */
    public BigDecimal getBitcoinToDollar() {
        BigDecimal rate = new BigDecimal("-1");
        String respJson = HttpUtil.sendGet(URL, "symbol=usdt_btc&appKey=59328e10e296a&sign=0a8d39b515fd8f3f8b848a4c459884c2");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        DigifinexRate digifinexRate = new Gson().fromJson(respJson, DigifinexRate.class);
        if (digifinexRate == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }

        BigDecimal dataLast = digifinexRate.getTicker().getLast();
        if (dataLast == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        logger.info("[D网内部接口]比特币与美元汇率:" + dataLast);
        rate = dataLast;
        return rate;
    }


    /**
     * @author drj
     * @date 2018/07/31 20:45
     * @Description:算力币对美元的汇率
     */
    public BigDecimal getBhpcToDollar() {
        BigDecimal rate = new BigDecimal("-1");
        String respJson = HttpUtil.sendGet(URL, "symbol=eth_bhpc&appKey=59328e10e296a&sign=0a8d39b515fd8f3f8b848a4c459884c2");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        DigifinexRate digifinexRate = new Gson().fromJson(respJson, DigifinexRate.class);
        if (digifinexRate == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }

        BigDecimal dataLast = digifinexRate.getTicker().getLast();
        if (dataLast == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        rate = dataLast.multiply(getEthereumToDollar()).setScale(6, BigDecimal.ROUND_DOWN);
        logger.info("[D网内部接口]算力币对美元汇率:" + dataLast);
        return rate;
    }


    /**
     * @author drj
     * @date 2018/07/31 20:42
     * @Description:获取人民币对美元的汇率
     */
    public BigDecimal cnyToDollarRate() {
        BigDecimal rate = new BigDecimal("-1");
        String respJson = HttpUtil.sendGet(DOLLAR_TO_CNY_URL, "");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        BcexCnyResp bcexCnyResp = new Gson().fromJson(respJson, BcexCnyResp.class);
        if (bcexCnyResp == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }
        String dollar2cny = bcexCnyResp.getUsd2cnyrate(); //获取美元对人民币的汇率
        if (ParamUtil.isBlack(dollar2cny)) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }

        rate = new BigDecimal("1").divide(new BigDecimal(dollar2cny), 6, BigDecimal.ROUND_DOWN);
        logger.info("[D网内部接口]人民币对美元的汇率:" + rate);
        return rate;
    }

    /**
     * @author zwj
     * @date 2018-08-01 16:44
     * @Description:获取比特币砖石对美元的汇率
     */
    public BigDecimal getDiamoundToDollar() {
        BigDecimal rate = new BigDecimal("-1");

        String respJson = HttpUtil.sendGet(BCEX_WALLET_TO_Dollar_URL, "");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        BcexResp bcexResp = new Gson().fromJson(respJson, BcexResp.class);
        if (bcexResp == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }
        if (bcexResp.getCode() != 0) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        Data data = bcexResp.getData();
        if (data == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }

        List<Btc> btcs = data.getBtc();
        if (btcs == null || btcs.size() == 0) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        for (Btc btc : btcs) {
            if ("bcd".equalsIgnoreCase(btc.getCoin_from())) {
                BigDecimal diamoundToBtc = new BigDecimal(btc.getPreprice());
                logger.info("[bcex外部接口]比特币砖石对比特币的汇率:" + diamoundToBtc);
                rate = diamoundToBtc;
            }
        }
        rate = rate.multiply(getBitcoinToDollar()).setScale(6, BigDecimal.ROUND_DOWN);
        return rate;
    }

    /**
     * @author zwj
     * @date 2018-08-02 09:48
     * @Description:获取布比币对美元的汇率
     */
    public BigDecimal getBuToDollar() {
        BigDecimal rate = new BigDecimal("-1");

        String respJson = HttpUtil.sendGet(BCEX_WALLET_TO_Dollar_URL, "");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        BcexResp bcexResp = new Gson().fromJson(respJson, BcexResp.class);
        if (bcexResp == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }
        if (bcexResp.getCode() != 0) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        Data data = bcexResp.getData();
        if (data == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }

        List<Eth> eths = data.getEth();
        if (eths == null || eths.size() == 0) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        for (Eth eth : eths) {
            if ("bu".equalsIgnoreCase(eth.getCoin_from())) {
                BigDecimal buToEth = new BigDecimal(eth.getPreprice());
                rate = buToEth;
            }
        }
        rate = rate.multiply(getEthereumToDollar()).setScale(6, BigDecimal.ROUND_DOWN);
        logger.info("[bcex外部接口]布比对美元的汇率:" + rate);
        return rate;
    }
}
